using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace CSSFriendly
{
	/// <summary>
	/// Provides static helper methods to CSSFriendly controls. Singleton instance.
	/// </summary>
	public class Helpers
	{
		/// <summary>
		/// Private constructor forces singleton.
		/// </summary>
		private Helpers()
		{
		}

		public static int GetListItemIndex(ListControl control, ListItem item)
		{
			int index = control.Items.IndexOf(item);
			if (index == -1)
				throw new NullReferenceException("ListItem does not exist ListControl.");

			return index;
		}

		public static string GetListItemClientID(ListControl control, ListItem item)
		{
			if (control == null)
				throw new ArgumentNullException("Control can not be null.");
			
			int index = GetListItemIndex(control, item);

			return String.Format("{0}_{1}", control.ClientID, index.ToString());
		}

		public static string GetListItemUniqueID(ListControl control, ListItem item)
		{
			if (control == null)
				throw new ArgumentNullException("Control can not be null.");

			int index = GetListItemIndex(control, item);

			return String.Format("{0}${1}", control.UniqueID, index.ToString());
		}

		public static bool HeadContainsLinkHref(Page page, string href)
		{
			if (page == null)
				throw new ArgumentNullException("page");

			foreach (Control control in page.Header.Controls)
			{
				if (control is HtmlLink && (control as HtmlLink).Href == href)
					return true;
			}

			return false;
		}

		public static void RegisterEmbeddedCSS(string css, Type type, Page page)
		{
			string filePath = page.ClientScript.GetWebResourceUrl(type, css);

			// if filePath is not empty, embedded CSS exists -- register it
			if (!String.IsNullOrEmpty(filePath))
			{
				if (!Helpers.HeadContainsLinkHref(page, filePath))
				{
					HtmlLink link = new HtmlLink();
					link.Href = page.ResolveUrl(filePath);
					link.Attributes["type"] = "text/css";
					link.Attributes["rel"] = "stylesheet";
					page.Header.Controls.Add(link);
				}
			}
		}
	}
}
